#!/usr/bin/env ruby -w

=begin
Copyright (c) 2007 RubyMSN team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=end

require 'msn/msn'

#################################################### EXAMPLE CODE ####################################################
# This example should provide enough information about creating a simple MSN client or bot. For more info, scroll up #
# and look at the classes ;-) HAVE FUN!!!                                                                            #
######################################################################################################################

if ARGV.length != 2
	puts "Usage: "+$0+" <email_address> <password>"
	exit
end

email = ARGV[0]
password = ARGV[1]

puts "Signing in... please wait."

# create a new MSN connection
msnsock = MSNConnection.new(email, password)

# connect events
msnsock.signed_in = lambda { puts "Signed in" }
msnsock.buddy_update = lambda {|oldcontact, contact| puts "Updated #{contact.email} (status: #{oldcontact.status.name} -> #{contact.status.name})"}

msnsock.new_chat_session = lambda {|tag, session|
	puts "New chat session request. Tag: #{tag}"
	# chat sessions have events too!
	session.message_received = lambda {|sender, message|
		puts sender + " says: " + message
		# just for fun ;-) The possibilities are endless...
		if message[0..3].upcase == "PING"
			message = message.gsub(/i/, "o").gsub(/\?/, "!")
			session.say message
		else
			session.say message.reverse
		end
	}
	session.session_started = lambda {
		puts "Session with tag '" + tag + "' started!"
		session.say "Moet je niet studeren voor IMCS/GS/AudioVerwerking ofzo?"
	}
	session.participants_updated = lambda {
		puts "Participants in '#{tag}': " + session.participants.list.to_s
	}
	
	# don't forget to start the chat session!
	session.start
}

# start signing in!
msnsock.start

# a very ugly coded but working basic msn chat interface!
while true
	sleep 1
	puts "Syntax: contacts OR start<tab>email OR chatroom-tag<tab>message"
	input = STDIN.gets.chop
	tag = input.split("\t")[0]
	if tag == "start"
		puts "Enter a tag so you can refer to the room: "
		msnsock.start_chat(STDIN.gets.chop, input.split("\t")[1])
	elsif tag == "contacts"
		msnsock.contactlists["FL"].list.each {|email, contact|
			puts email + ": " + contact.status.name if contact.status.name != "Offline"
		}
	else
		message = input.split("\t")[1]
		msnsock.chatsessions[tag].say(message)
	end
end

